爬蟲暗黑編年史的世界王時間
睡睡念
好久沒寫爬蟲了,
最近玩暗黑IV,開始接觸到世界王的時候,時間很不固定,
然後隔了幾天發現暗黑編年史,
有世界王的通知時間倒數了。
有資料就好辦了。
正文
- 開啓scrapy專案
scrapy start <project_name>
- 初始化(在上一個步驟執行完後,就有相關指令會顯示在螢幕上)
scrapy genspider d4spider https://diablo4.cc/tw/
結構如下
├── LICENSE
├── README.md
├── d4notify
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-310.pyc
│ │ └── settings.cpython-310.pyc
│ ├── items.py
│ ├── middlewares.py
│ ├── pipelines.py
│ ├── settings.py
│ └── spiders
│ ├── __init__.py
│ ├── __pycache__
│ │ └── __init__.cpython-310.pyc
│ └── d4spider.py
└── scrapy.cfg
- 執行
scrapy crawl d4spider
Troubleshooting
- 取得屬性
碰到個奇怪的語法,div裏面有個自訂的屬性,
用scrapy要取值的話,
<div data-displaytime="1687258903">2023-06-20 19:01</div>
div::attr(data-displaytime).get()
這樣會取得 1687258903 這個數字
ref. scrapy-scraping-html-custom-attributes
- extract_first() 跟 extract() ,可用get()跟getall()取代
ref. 選擇器(Selectors)